﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Diagnostics;
using System.Text;

namespace Xceed.Silverlight.Data
{
  [DebuggerDisplay( "Filter = {DebuggerDisplayString}" )]
  public sealed class FilterRequest
  {
    #region Constructors

    internal FilterRequest( Predicate<object> predicate )
      : this( predicate, null )
    {
    }

    internal FilterRequest( FilterExpressionBase expression )
      : this( null, expression )
    {
    }

    internal FilterRequest( Predicate<object> predicate, FilterExpressionBase expression )
    {
      if( ( predicate == null ) && ( expression == null ) )
        throw new ArgumentException( "At least one filter must be specified." );

      m_filter = predicate;
      m_filterExpression = expression;
    }

    #endregion

    #region Expression Property

    public FilterExpressionBase Expression
    {
      get
      {
        return m_filterExpression;
      }
    }

    private FilterExpressionBase m_filterExpression; //null

    #endregion

    #region Filter Property

    internal Predicate<object> Filter
    {
      get
      {
        return m_filter;
      }
    }

    private Predicate<object> m_filter; //null

    #endregion

    #region DebuggerDisplayString Private Property

    // This property only purpose is to generate a string for the DebuggerDisplay attribute.
    private string DebuggerDisplayString
    {
      get
      {
        StringBuilder sb = new StringBuilder();

        if( m_filter != null )
        {
          sb.Append( "{Predicate}" );
        }

        if( m_filterExpression != null )
        {
          if( sb.Length != 0 )
          {
            sb.Append( " And " );
          }

          sb.Append( m_filterExpression.DebuggerDisplayString );
        }

        return ( m_filterExpression != null )
                 ? m_filterExpression.DebuggerDisplayString
                 : string.Empty;
      }
    }

    #endregion
  }
}
